package com.CNMedical.system.service.impl;

import java.util.List;

import com.diboot.core.service.impl.BaseServiceImpl;
import com.CNMedical.common.core.domain.R;
import com.CNMedical.common.utils.DateUtils;
import com.CNMedical.common.utils.StringUtils;
import com.CNMedical.system.domain.entity.AppletQuestion;
import com.CNMedical.system.service.IAppletQuestionOptionService;
import com.CNMedical.system.service.IAppletQuestionService;
import org.springframework.stereotype.Service;
import com.CNMedical.system.mapper.AppletQuestionAnswerMapper;
import com.CNMedical.system.domain.entity.AppletQuestionAnswer;
import com.CNMedical.system.service.IAppletQuestionAnswerService;

import javax.annotation.Resource;

/**
 * 试题-答案关联Service业务层处理
 * 
 * @author tang
 * @date 2023-03-28
 */
@Service
public class AppletQuestionAnswerServiceImpl extends BaseServiceImpl<AppletQuestionAnswerMapper, AppletQuestionAnswer> implements IAppletQuestionAnswerService
{
    @Resource
    private AppletQuestionAnswerMapper appletQuestionAnswerMapper;

    @Resource
    private IAppletQuestionService appletQuestionService;

    @Resource
    private IAppletQuestionOptionService appletQuestionOptionService;

    @Override
    public double countScore(List<Long> questionIdList, String userId) {
        List<AppletQuestionAnswer> list = lambdaQuery().eq(AppletQuestionAnswer::getUserId, userId)
                .in(AppletQuestionAnswer::getQuestionId, questionIdList)
                .gt(AppletQuestionAnswer::getScore, 0).list();
        double score = list.stream().mapToDouble(AppletQuestionAnswer::getScore).sum();
        return score;
    }

    @Override
    public R finishExam(String knowledgeId, String userId) {
        lambdaQuery().eq(AppletQuestionAnswer::getKnowledgeId,knowledgeId)
                .eq(AppletQuestionAnswer::getUserId,userId).list();
        return null;
    }

    @Override
    public R putQuestionAnswer(String questionId, String optionId, String userId) {
        boolean exists = lambdaQuery().eq(AppletQuestionAnswer::getQuestionId, questionId)
                .eq(AppletQuestionAnswer::getUserId, userId).exists();
        if (!exists) {
            String b = appletQuestionOptionService.whetherCorrectOption(questionId, optionId);
            AppletQuestion appletQuestion = appletQuestionService.selectAppletQuestionById(Long.valueOf(questionId));
            if (StringUtils.equals(b,"1")) {
                this.save(AppletQuestionAnswer.builder().answerId(optionId)
                        .userId(userId)
                        .questionId(questionId)
                        .score(appletQuestion.getScore()).build());
            }else {
                this.save(AppletQuestionAnswer.builder().answerId(optionId)
                        .userId(userId)
                        .questionId(questionId)
                        .score(0).build());
            }
        } else {
            lambdaUpdate().eq(AppletQuestionAnswer::getQuestionId, questionId)
                    .eq(AppletQuestionAnswer::getUserId, userId)
                    .set(AppletQuestionAnswer::getAnswerId,optionId).update();
        }
        return R.ok();
    }

    /**
     * 查询试题-答案关联
     * 
     * @param id 试题-答案关联主键
     * @return 试题-答案关联
     */
    @Override
    public AppletQuestionAnswer selectAppletQuestionAnswerById(Long id)
    {
        return appletQuestionAnswerMapper.selectAppletQuestionAnswerById(id);
    }

    /**
     * 查询试题-答案关联列表
     * 
     * @param appletQuestionAnswer 试题-答案关联
     * @return 试题-答案关联
     */
    @Override
    public List<AppletQuestionAnswer> selectAppletQuestionAnswerList(AppletQuestionAnswer appletQuestionAnswer)
    {
        return appletQuestionAnswerMapper.selectAppletQuestionAnswerList(appletQuestionAnswer);
    }

    /**
     * 新增试题-答案关联
     * 
     * @param appletQuestionAnswer 试题-答案关联
     * @return 结果
     */
    @Override
    public int insertAppletQuestionAnswer(AppletQuestionAnswer appletQuestionAnswer)
    {
        appletQuestionAnswer.setCreateTime(DateUtils.getNowDate());
        return appletQuestionAnswerMapper.insertAppletQuestionAnswer(appletQuestionAnswer);
    }

    /**
     * 修改试题-答案关联
     * 
     * @param appletQuestionAnswer 试题-答案关联
     * @return 结果
     */
    @Override
    public int updateAppletQuestionAnswer(AppletQuestionAnswer appletQuestionAnswer)
    {
        appletQuestionAnswer.setUpdateTime(DateUtils.getNowDate());
        return appletQuestionAnswerMapper.updateAppletQuestionAnswer(appletQuestionAnswer);
    }

    /**
     * 批量删除试题-答案关联
     * 
     * @param ids 需要删除的试题-答案关联主键
     * @return 结果
     */
    @Override
    public int deleteAppletQuestionAnswerByIds(Long[] ids)
    {
        return appletQuestionAnswerMapper.deleteAppletQuestionAnswerByIds(ids);
    }

    /**
     * 删除试题-答案关联信息
     * 
     * @param id 试题-答案关联主键
     * @return 结果
     */
    @Override
    public int deleteAppletQuestionAnswerById(Long id)
    {
        return appletQuestionAnswerMapper.deleteAppletQuestionAnswerById(id);
    }
}
